﻿using LibVLCSharp.Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WeighClient.Windows
{
    /// <summary>
    /// VlcWindow.xaml 的交互逻辑
    /// </summary>
    public partial class VlcWindow : Window
    {
        private LibVLC _libVLC;
        private LibVLCSharp.Shared.MediaPlayer _mediaPlayer;
        private string VlcIp;
        public VlcWindow()
        {
            InitializeComponent();
            InitializeVLC();
            PlayRtspStream();
            LibVLC _libVlc;
        }
        private void InitializeVLC()
        {
            Core.Initialize();
            _libVLC = new LibVLC();
            _mediaPlayer = new LibVLCSharp.Shared.MediaPlayer(_libVLC);
            VideoViewControl.MediaPlayer = _mediaPlayer;
        }

        private void PlayRtspStream()
        {
            VlcIp = App.GetConfig().VlcIp;
            
            // 替换为你的摄像头RTSP地址
            var rtspUrl = VlcIp;

            using (var media = new Media(_libVLC, rtspUrl, FromType.FromLocation))
            {
                // 设置网络缓存（单位：毫秒）
                media.AddOption(":network-caching=200");
                _mediaPlayer.Play(media);
            }
        }

        // 窗口关闭时释放资源
        protected override void OnClosed(EventArgs e)
        {
            _mediaPlayer?.Stop();
            _mediaPlayer?.Dispose();
            _libVLC?.Dispose();
            base.OnClosed(e);
        }
    }
}
